#!/bin/bash
echo "
############################
##     Running MCV        ##
############################"
current_time=$1

start_year=`echo $current_time | cut -b1-4`
start_month=`echo $current_time | cut -b5-6`
start_day=`echo $current_time | cut -b7-8`
start_hour=`echo $current_time | cut -b9-10`

resolution=$2
imp_physics=$3
case_dir=$4
MCVdir=$5
MPSdir=$6
##--------------------------------------------------------

cd ${case_dir}
echo 'Runing MCV in '`pwd`
# copy the directory structure to case_dir form MCVdir except for the 'job_logs' and 'run' directories!
echo 'Copy directory structure to '$(pwd)
rsync -av --progress ${MCVdir}/ ${case_dir} --exclude run --exclude job_logs >/dev/null 2>&1
cd ${MCVdir}/run && find . -type d -exec mkdir -p -- ${case_dir}/run/{} \;

# Copy required files
echo 'Copy required files'
cp -fr ${MCVdir}/CCPL_dir/* ${case_dir}/CCPL_dir/
cp -fr ${MCVdir}/config/* ${case_dir}/config/*
cp -f ${MCVdir}/run/atm/MCV/exe/MCV ${case_dir}/run/atm/MCV/exe/
cp -f ${MCVdir}/run/atm/MCV/data/link_clim.sh ${case_dir}/run/atm/MCV/data/link_clim.sh
cp -f ${MCVdir}/run/atm/MCV/data/make_input_nml.sh ${case_dir}/run/atm/MCV/data/make_input_nml.sh
cp -f ${MCVdir}/run/atm/MCV/data/namelist.atm ${case_dir}/run/atm/MCV/data/namelist.atm
cp -f ${MCVdir}/run/atm/MCV/data/data_table ${case_dir}/run/atm/MCV/data/data_table
cp -f ${MCVdir}/run/atm/MCV/data/field_table-gfdl-mp ${case_dir}/run/atm/MCV/data/field_table-gfdl-mp
cp -f ${MCVdir}/run/atm/MCV/data/field_table-zhao-mp ${case_dir}/run/atm/MCV/data/field_table-zhao-mp

echo 'source setenv.sh and ./configure'
cd ${case_dir}
source setenv.sh
./configure >/dev/null 2>&1

cd ${case_dir}/run/atm/MCV/data/
#1) Link static data, make input.nml and field_table
sh link_clim.sh
sh make_input_nml.sh ${resolution} ${imp_physics}
#2) Get the initial data cooked by MPS
echo 'Link the initial data'
initial_file=${MPSdir}/run/mcv_input_${start_year}${start_month}${start_day}${start_hour}0000.nc
if [ -e ${initial_file} ]
then
  ln -sf ${initial_file} .
else
  echo "Error: ${initial_file} does not exist!"
  exit
fi

#3) Set the namelist.atm
echo 'Modify the namelist.atm'
sed -ri 's/([[:blank:]]*start_year[[:blank:]]*=) (.*)/\1 '"${start_year}"'/g'   ./namelist.atm
sed -ri 's/([[:blank:]]*start_month[[:blank:]]*=) (.*)/\1 '"${start_month}"'/g' ./namelist.atm
sed -ri 's/([[:blank:]]*start_day[[:blank:]]*=) (.*)/\1 '"${start_day}"'/g'     ./namelist.atm
sed -ri 's/([[:blank:]]*start_hour[[:blank:]]*=) (.*)/\1 '"${start_hour}"'/g'   ./namelist.atm
sed -ri 's/([[:blank:]]*mpscheme[[:blank:]]*=) (.*)/\1 '"${imp_physics}"'/g'    ./namelist.atm

#4) Modify the 'start_data' in env_run.xml
echo 'Modify the env_run.xml'
start_date=${start_year}${start_month}${start_day}
sed -ri "s/(\sstart_date=\s*)(.*)/\1\""${start_date}"\"/g"   ${case_dir}/CCPL_dir/config/all/env_run.xml
if [ ${start_hour} == '18' ];then
  sed -ri "s/(\sstart_second=\s*)(.*)/\1\""64800"\"/g" ${case_dir}/CCPL_dir/config/all/env_run.xml
elif [ ${start_hour} == '12' ];then
  sed -ri "s/(\sstart_second=\s*)(.*)/\1\""43200"\"/g" ${case_dir}/CCPL_dir/config/all/env_run.xml
elif [ ${start_hour} == '06' ];then
  sed -ri "s/(\sstart_second=\s*)(.*)/\1\""21600"\"/g" ${case_dir}/CCPL_dir/config/all/env_run.xml
elif [ ${start_hour} == '00' ];then
  sed -ri "s/(\sstart_second=\s*)(.*)/\1\""0"\"/g" ${case_dir}/CCPL_dir/config/all/env_run.xml
fi
#5) Run case
#cd ${MCVdir}
#./runcase >/dev/null 1>&1
echo 'create a sbatch job script into' ${case_dir}/job_logs
mkdir -p ${case_dir}/job_logs
cat > ${case_dir}/job_logs/job.submit.${current_time} << EOF
#!/bin/bash
#SBATCH --comment=GRAPES
#SBATCH --job-name=GRAPES
#SBATCH --wckey=${wckey}
#SBATCH -n ${nodes}
#SBATCH -c 1
#SBATCH --ntasks-per-node=64
#SBATCH -p normal
#SBATCH -o ${case_dir}/job_logs/PMCV.output.${current_time}.%J
#SBATCH -e ${case_dir}/job_logs/PMCV.error.${current_time}.%J
#SBATCH -J PMCV
#SBATCH --time 3-00:00:00

#set runtime environment variables
export OMP_NUM_THREADS=1
export PNETCDF_HINTS="nc_header_align_size=1048576"
export I_MPI_PIN_RESPECT_CPUSET=0

#some shell commands

ulimit -s unlimited
mpirun "${case_dir}/run/atm/MCV/exe/MCV" "${case_dir}/run/all/MCV.nml"
EOF

cd ${case_dir}
sbatch ${case_dir}/job_logs/job.submit.${current_time} > ${case_dir}/job_logs/jobid.txt
